package com.fox.limit;

import com.fox.lock.DistributedLockService;
import com.fox.lock.SingleTask;
import com.fox.redisbase.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


@Component
public class RedisCurrentLimiting implements ApplicationRunner {
    private static Logger logger = LoggerFactory.getLogger(RedisCurrentLimiting.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private DistributedLockService distributedLockService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        SingleTask singleTask = new SingleTask(distributedLockService, "RedisCurrentLimitingReset") {
            @Override
            public void excuteTask() {
                    while (resetThreadAlive()) {
                        redisService.set(LimitConstant.MATRIX_LIMIT_KEY, LimitConstant.CONCURRENCY_NUM);
                        try {
                            Thread.sleep(6000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                     System.out.println("重置线程挂了 退出循环");

            }
        };
        singleTask.start();
    }
}
